/**
 * @projectName study-note
 * @package middle
 * @className middle.Test
 */
package middle;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Test
 * @description
 * @author answer_wx
 * @date 2022/7/11 21:49
 * @version 1.0
 */
public class Test {
    public static void method(int n) {
        int[][] nums = new int[n][n];
        int s = nums[0][0] = 1;
        int i = 0, j = 0;
        while (s < n * n) {
            while (j + 1 < n && nums[i][j + 1] == 0) {
                nums[i][++j] = ++s;
            }
            while (i + 1 < n && nums[i + 1][j] == 0) {
                nums[++i][j] = ++s;
            }
            while (j > 0 && nums[i][j - 1] == 0) {
                nums[i][--j] = ++s;
            }
            while (i > 0 && nums[i - 1][j] == 0) {
                nums[--i][j] = ++s;
            }
        }
        for (int k = 0; k < nums.length; k++) {
            for (int l = 0; l < nums[k].length; l++) {
                System.out.print(nums[k][l] + ",");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        method(10);
        CopyOnWriteArrayList
    }
}